package com.etone.smartAudit.controller;


import cn.hutool.core.util.StrUtil;
import com.etone.smartAudit.annotation.FormAop;
import com.etone.smartAudit.annotation.LoginAdmin;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.domain.CertificateFile;
import com.etone.smartAudit.domain.CertificatePerson;
import com.etone.smartAudit.dto.certificate.*;
import com.etone.smartAudit.error.CustomParameterizedException;
import com.etone.smartAudit.security.jwt.JWTConfigurer;
import com.etone.smartAudit.service.CertificateFileService;
import com.etone.smartAudit.vo.certificate.CertificateVo;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.certificate.CertificateVoDetail;
import com.etone.smartAudit.vo.certificate.PersonLoginInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 存证单表  前端控制器
 * </p>
 *
 * @author jiayen
 * @since 2021-06-08
 */
@RestController
@Api(value = "存证单(需求单与确认单)", tags = "存证单(需求单与确认单)")

public class CertificateFileController {

    @Autowired
    private CertificateFileService fileService;

    @FormAop
    @PostMapping("/certificateFile/add")
    @ApiOperation(value = "增加需求单或者确认单", tags = "jiayen")
    public void addCetificateFile(@ApiIgnore @LoginAdmin Admin admin, @RequestBody AddCertificateDTO dto) {
        valitedType(dto.getType());
        if (dto.getId() != null) {
            throw new CustomParameterizedException("添加参数错误!");
        }
        fileService.addCetificateFile(admin, dto);
    }


    @PutMapping("/certificateFile/edit")
    @ApiOperation(value = "修改需求单或者确认单", tags = "jiayen")
    public void editCetificateFile(@ApiIgnore @LoginAdmin Admin admin, @RequestBody AddCertificateDTO dto) {
        valitedType(dto.getType());
        if (dto.getId() == null) {
            throw new CustomParameterizedException("存证单id不能为空");
        }
        fileService.editCetificateFile(admin, dto);
    }

    @GetMapping("/certificateFile/{type}/list")
    @ApiOperation(value = "需求单或者确认单列表", tags = "jiayen")
    public PagerVO<CertificateVo> listCetificateFile(@ApiIgnore @LoginAdmin Admin admin
            , @ApiParam("存证单类型 1-需求单，2-确认单") @PathVariable("type") Integer type
            , CertificateDTO certificateDTO) {
        valitedType(type);
        certificateDTO.setType(type);
        return fileService.listCetificateFile(admin, certificateDTO);
    }

    @GetMapping("/certificateFile/{id}")
    @ApiOperation(value = "需求单或者确认单详情", tags = "jiayen")
    public CertificateVoDetail cetificateFileDetail(@ApiIgnore @LoginAdmin Admin admin
            , @ApiParam("主键id") @PathVariable(value = "id") Integer id) {

        return fileService.cetificateFileDetail(admin, id);
    }

    @GetMapping("/certificateFile/fileSure/{id}")
    @ApiOperation(value = "确认单详情", tags = "jiayen")
    public List<CertificateVoDetail> cetificateFileSureDetail(@ApiIgnore @LoginAdmin Admin admin
            , @ApiParam("主键id") @PathVariable(value = "id") Integer id) {

        return fileService.cetificateFileSureDetail(admin, id);
    }

    @DeleteMapping("/certificateFile/{id}")
    @ApiOperation(value = "删除需求单或者确认单", tags = "jiayen")
    public void deleteFile(@ApiIgnore @LoginAdmin Admin admin
            , @ApiParam("主键id") @PathVariable(value = "id") Integer id) {
        fileService.deleteFile(id);
    }


    /**
     * 获取h5详情
     *
     * @param admin
     * @param
     * @return
     */
    @PostMapping("/certificateFile/wap/detail/{id}")
    @ApiOperation(value = "获取h5详情信息", tags = "jiayen")
    public PersonLoginInfo wapDetail(@ApiIgnore @LoginAdmin Admin admin
            , @ApiParam("主键id") @PathVariable(value = "id") Integer id) {
        return fileService.wapDetail(admin, id);
    }


    /***
     * 不需要登录授权
     * @param dto
     * @return
     */
    @PostMapping("/certificateFile/noAuth/login")
    @ApiOperation(value = "执行人登录", tags = "jiayen")
    public PersonLoginInfo loginCertificate(@RequestBody CertificatePersonDTO dto) {
        return fileService.loginCertificate(dto);
    }

    /***
     * 不需要登录授权
     * @param dto
     * @return
     */
    @PostMapping("/certificateFile/noAuth/handPerson")
    @ApiOperation(value = "执行审批或者扭转", tags = "jiayen")
    public CertificatePerson handCertificate(HttpServletRequest request, @RequestBody CertificateHandDTO dto) {
        if (StrUtil.isEmpty(dto.getToken())) {
            String loginToken = request.getHeader(JWTConfigurer.CERAUTHORIZATION_HEADER);
            dto.setToken(loginToken);
        }
        return fileService.handCertificate(dto);
    }


    /***
     * 不需要登录授权
     * 审批使用portal账号信息
     * @param dto
     * @return
     */
    @PostMapping("/certificateFile/portal/handPerson")
    @ApiOperation(value = "执行审批或者扭转", tags = "jiayen")
    public CertificatePerson handCertifacePortal(HttpServletRequest request, @RequestBody CertificateHandPortalDTO dto) {
        if (StrUtil.isEmpty(dto.getToken())) {
            String loginToken = request.getHeader(JWTConfigurer.CERAUTHORIZATION_HEADER);
            dto.setToken(loginToken);
        }
        return fileService.handCertificatePortal(dto);
    }

    @PostMapping("/certificateFile/checkedCerFile/{id}")
    @ApiOperation(value = "审核需求单或者确认单", tags = "jiayen")
    public void checkedCerFile(@ApiIgnore @LoginAdmin Admin admin
            , @ApiParam("主键id") @PathVariable(value = "id") Integer id
            , @ApiParam(value = "审核状态，0-未审核，1-审核完成,2-审核不同意") @RequestParam Integer checkStatus) {
        fileService.checkedCerFile(admin, id, checkStatus);
    }

    @PostMapping("/certificateFile/reback/{id}")
    @ApiOperation(value = "撤回需求单或者确认单", tags = "jiayen")
    public void reback(@ApiIgnore @LoginAdmin Admin admin
            , @ApiParam("主键id") @PathVariable(value = "id") Integer id
    ) {
        fileService.rebackFile(admin, id);
    }

    @PostMapping("/certificateFile/compelet/{id}")
    @ApiOperation(value = "完结工单", tags = "jiayen")
    public void compelet(@ApiIgnore @LoginAdmin Admin admin
            , @ApiParam("主键id") @PathVariable(value = "id") Integer id
    ) {
        fileService.compelet(admin, id);
    }


    /***
     * 验证存在单类型
     * @param type
     */
    private void valitedType(Integer type) {
        if (type == null) {
            throw new CustomParameterizedException("请输入正确的参数!");
        }
        if (!CertificateFile.NEED_TYPE.equals(type) && !CertificateFile.CONFIRM_TYPE.equals(type)) {
            throw new CustomParameterizedException("存证单类型不正确!");
        }
    }

    /***
     * 转发到h5页面
     * @return
     */
    public String toH5() {

        return null;
    }
}
